/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022.  All rights reserved.2023.
 */

package service

import (
	"errors"
	"fmt"
	"prim-server/common"
	"prim-server/dao"
	"prim-server/logger"
	"prim-server/model"
	"prim-server/util"
)

// 增加用户资源
func AddAsset(userId, cloudUserId, productionType, resourceType, cloudResourceId,
	associatedOrderId, associatedCloudOrderId string) error {
	asset := &model.Asset{
		AssetId:                util.GetUUID(),
		UserId:                 userId,
		CloudUserId:            cloudUserId,
		ProductionType:         productionType,
		ResourceType:           resourceType,
		CloudResourceId:        cloudResourceId,
		AssociatedOrderId:      associatedOrderId,
		AssociatedCloudOrderId: associatedCloudOrderId,
	}
	err := dao.GetAssetDao().SaveAssetInfo(asset)
	if err != nil {
		logger.Error("add asset error: %v, userId: %s, assetId: %s", err.Error(), userId, asset.AssetId)
		return errors.New("增加用户资源失败")
	}
	return nil
}

// 删除用户资源
func DeleteAsset(assertId string) error {
	err := dao.GetAssetDao().DeleteAssetInfo(assertId)
	if err != nil {
		logger.Error("delete asset error: %v, assertId: %s", err.Error(), assertId)
		return errors.New("删除用户资源失败")
	}
	return nil
}

// 查询用户资源列表
func GetAssetWithUserId(userId string) ([]*model.Asset, error) {
	queryParameter := &common.QueryAssetDetail{
		UserId: []string{userId},
	}
	assets, _, err := GetAssetAllWithParameter(queryParameter)
	if err != nil {
		logger.Error("get asset error: %v, userId: %s", err.Error(), userId)
		return nil, errors.New("查询资源更新记录失败")
	}
	if len(assets) == 0 {
		return nil, nil
	}
	return assets, nil
}
func GetAssetOneWithAssetId(assetId string) (*model.Asset, error) {
	queryParameter := &common.QueryAssetDetail{
		AssetId: []string{assetId},
	}
	assets, _, err := GetAssetAllWithParameter(queryParameter)
	if err != nil {
		logger.Error("error: %s, assetId: %s", err.Error(), assetId)
		return nil, errors.New("查询资源更新记录失败")
	}
	if len(assets) == 0 {
		return nil, nil
	}
	return assets[0], nil
}
func GetAssetAll(assetId, userId, productionType, resourceType, cloudResourceId,
	associatedOrderId, associatedCloudOrderId []string, page, pageSize *int) ([]*model.Asset, int, error) {
	queryParameter := &common.QueryAssetDetail{
		AssetId:                assetId,
		UserId:                 userId,
		ProductionType:         productionType,
		ResourceType:           resourceType,
		CloudResourceId:        cloudResourceId,
		AssociatedOrderId:      associatedOrderId,
		AssociatedCloudOrderId: associatedCloudOrderId,
		Page:                   page,
		PageSize:               pageSize,
	}
	return GetAssetAllWithParameter(queryParameter)
}
func GetAssetAllWithParameter(queryParameter *common.QueryAssetDetail) ([]*model.Asset, int, error) {
	if queryParameter == nil {
		return nil, 0, nil
	}
	assets, totalCount, err := dao.GetAssetDao().GetAssetInfo(queryParameter)
	if err != nil {
		logger.Error("get error: %s, userId: %s", err.Error(), fmt.Sprint(queryParameter.UserId))
		return nil, 0, errors.New("查询用户资源失败")
	}
	return assets, totalCount, nil
}

// 更新用户资源列表--指定资源更新资源状态
func UpdateAssetWithStatus(assertId string, resourceStatus string) error {
	asset, err := GetAssetOneWithAssetId(assertId)
	if err != nil || asset == nil {
		logger.Error("update asset error:%s, assetId: %s, resourceStatus: %s", err.Error(), assertId, resourceStatus)
		return errors.New("更新用户资源失败")
	}
	asset.ResourceStatus = resourceStatus
	err = dao.GetAssetDao().UpdateAssetInfo(asset)
	if err != nil {
		logger.Error("update asset error:%s, assetId: %s, resourceStatus: %s", err.Error(), assertId, resourceStatus)
		return errors.New("更新用户资源失败")
	}
	return nil
}
